@extends('frontend/layouts/bootstrap')

@section('content')
<h1>Машины жагсаалт - Document</h1>
@if(Session::has('success_car'))
    @foreach(Session::get('success_car') as $car => $key)
        @if ($key == 1)
            <span class="badge badge-success"><i class="icon icon-ok"></i></span> {{ $car }} </br>
        @else
            <span class="badge badge-important"><i class="icon icon-remove"></i></span> {{ $car }} --> <i class="icon icon-dollar"></i>{{ $key }}</br>
        @endif
    @endforeach
    {{ Session::forget('success_car') }}
@endif

{{-- FILTER --}}
<form class="form-inline" style="margin: 0;" action="{{ route('car_balance.main.fax_transmission_list_filter') }}" method="post">
    <table class="table table-bordered">
        <thead style="background-color: #f0f0f0;">
        <tr>
            <th>車体番号</th>
            <th>Number plate</th>
            <th>抹消日～</th>
            <th>FAX送付日～</th>
            <th>Action</th>
        </tr>
        </thead>

        <tbody>
        <tr>
            <td>
                <input id='chassis_no' placeholder="車体番号" type="text" name="chassis_no" class="input-small" value="{{ $filters['chassis_no'] ? $filters['chassis_no'] : '' }}"/>
            </td>
            <td>
                <input id="number_plate_flag_cb" placeholder="設定" type="checkbox" class="input-small" style="margin-bottom: 5px; margin-right: 5px;" name="number_plate_flag_cb" {{ $filters['number_plate_flag_cb'] ? 'checked' : '' }}/><label for="number_plate_flag_cb">設定</label>
            </td>
            <td>
                <input class="datepicker input-small" placeholder="抹消日" title="抹消日" name="burtgel_solih_date" id="burtgel_solih_date" type="text" value="{{ $filters['burtgel_solih_date'] }}" {{ $filters['burtgel_solih'] ? 'disabled' : '' }}>
                <input id="burtgel_solih" placeholder="未設定" type="checkbox" class="input-small" style="margin-bottom: 5px; margin-right: 5px;" onclick="enableDisable(this.checked, 'burtgel_solih_date')" name="burtgel_solih" {{ $filters['burtgel_solih'] ? 'checked' : '' }}/><label for="burtgel_solih">未設定</label>
            </td>
            <td>
                <input class="datepicker input-small" placeholder="FAX送付日" title="FAX送付日" name="fax_export_erasure_to_auction_date" id="fax_export_erasure_to_auction_date" type="text" value="{{ $filters['fax_export_erasure_to_auction_date'] }}" {{ $filters['fax_export_erasure_to_auction'] ? 'disabled' : '' }}>
                <input id="fax_export_erasure_to_auction" placeholder="未設定" type="checkbox" class="input-small" style="margin-bottom: 5px; margin-right: 5px;" onclick="enableDisable(this.checked, 'fax_export_erasure_to_auction_date')" name="fax_export_erasure_to_auction" {{ $filters['fax_export_erasure_to_auction'] ? 'checked' : '' }}/><label for="fax_export_erasure_to_auction">未設定</label>
            </td>
            <td>
                {{ Form::token() }}
                <button type="submit" class="btn">Apply Filter</button>
                <!--                <button type="reset" class="btn" onclick="reset()">Reset</button>-->
                <a class="btn" href="{{ route('car_balance.main.fax_transmission_list') }}?reset=1">Reset</a>
            </td>
        </tr>
        </tbody>
    </table>
</form>

@if ($cars->count())
<div>{{ $cars->getTotal() }} results.</div>
    <table class="table table-bordered table-condensed table-hover">
        <thead style="background-color: #f0f0f0;white-space: nowrap;">
        <tr>
            <th title="Action" style="width: 40px;">
                <input id="all_fax_flag" placeholder="チェック" type="checkbox" class="input-small" style="float: left; margin-top: 10px;" onclick="setAllFaxFlag(this.checked)" name="all_fax_flag" /><label style="float: left; padding-left: 5px; padding-top: 5px;" for="all_bill_flag">All</label>
            </th>
            <th title="Date">日付</th>
            <th title="Auction">オーク会場</th>
            <th title="Auction no">出品番号</th>
            <th title="Car model">車種</th>
            <th title="Chassis number">車体番号</th>
            <td title="Registration number">登録番号</td>
            <td title="Number Plate Flag">ナンバー到着</td>
            <td title="Export erasure image">届書Image</td>
            <td title="Day of Cancellation">抹消日</td>
            <td title="FAX sending Date">FAX送付日</td>

        </tr>
        </thead>
        <tbody style="font-size: 11px; white-space:nowrap;"">
        @foreach($cars as $car)
        <tr>
            <th >
                <input id="fax_{{ $car->id }}" placeholder="チェック" type="checkbox" class="fax_chbox input-small" style="margin-bottom: 5px; margin-right: 5px;" name="fax_flag"/>
            </th>
            <th>{{ $car->purchase_date }}</th>
            <th>{{ $car->auction_name }}</th>
            <th>{{ $car->lot_no }}</th>
            <th>{{ $car->car_maker }} {{ $car->car_name }}</th>
            <th>{{ $car->chasis_no; }}</th>
            <td>{{ $car->registration_number; }}</td>
            <td>
                @if ($car->number_plate_flag != 1 )
                    <span class="badge badge-inverse"><i class="icon icon-off"></i></span>
                @else
                    @if ($car->number_arrival_flag and $car->fax_export_erasure_to_auction )
                        <span class="badge badge-success"><i class="icon icon-ok"></i></span>
                    @else
                        <input id="number_arrival_flag" placeholder="チェック" type="checkbox" class="input-small" style="margin-bottom: 5px; margin-right: 5px;" onclick="setNumberArrival(this.checked, {{ $car->id }})" name="number_arrival_flag" {{  $car->number_arrival_flag ? 'checked' : '' }}/>
                    @endif
                @endif
            </td>
            @if ( $car->mail_export_erasure_to_nmc_upload_flag )
            <td><a target="_blank" style="margin-left: 25px;" class="btn btn-success btn-mini" href="{{route('car_balance.main.display', array('type'=>'export_cert', 'file'=>$car->chasis_no.'.jpg'))}}" title="{{ $car->car_year }}-{{ $car->car_name }}-{{ $car->chasis_no; }}"><i class="icon-zoom-in icon-white"></i></a></td>
            @else
            <td>No image upload</td>
            @endif
            <td>{{ $car->burtgel_solih; }}</td>
            <td>{{ $car->fax_export_erasure_to_auction; }}</td>
        </tr>
        @endforeach
        </tbody>
    </table>
@include('nimo/domains/car_balance/page')

<h4>Ерөнхий мэдээлэл</h4>
<table class="table table-bordered">
    <thead style="background-color: #f0f0f0;">
    <th title="From">From</th>
    <th title="Subject">Subject</th>
    <th title="E-mail body">E-mail body</th>
    <th title="E-mail signature">E-mail signature</th>
    </thead>
    <tr>
        <td>
            {{ Form::select('fax_account', $fax_account_list, array('class' => 'input-small', 'id'=>'fax_account')) }}
        </td>
        <td>
            <input class="input-medium" placeholder="Subject" title="Subject" id="subject" name="subject" type="text">
        </td>
        <td>
            <textarea placeholder="E-Mail Body" name="email_body" id="email_body" cols="100" rows="10" style="width: 450px;"></textarea>
        </td>
        <td>
            <textarea placeholder="E-Mail Signature" name="email_signature" id="email_signature" cols="50" rows="10"></textarea>
        </td>
    </tr>
</table>

<button class="btn btn-large btn-success" onclick="return faxSend()" ><i class="icon-plus icon-white"></i> Send</button>

@else
Хайлтад тохирох үр дүн алга
@endif

@stop


@section('pagestyles')
<link rel="stylesheet" type="text/css" href={{ asset('assets/chosen-bootstrap/chosen.min.css') }} />
<link rel="stylesheet" type="text/css" href={{ asset('assets/bootstrap-daterangepicker/daterangepicker.css') }} />
<link rel="stylesheet" type="text/css" href={{ asset('assets/lightbox/lightbox.css') }} />
<link rel="stylesheet" type="text/css" href={{ asset('assets/bootstrap-datepicker/datepicker.css') }} />
@stop

@section('pagescripts')
<!--page specific plugin scripts-->
<script type="text/javascript" src={{ asset('assets/chosen-bootstrap/chosen.jquery.min.js') }}></script>
<script type="text/javascript" src={{ asset('assets/bootstrap-daterangepicker/date.js') }} ></script>
<script type="text/javascript" src={{ asset('assets/bootstrap-daterangepicker/daterangepicker.js') }}></script>
<script type="text/javascript" src={{ asset('assets/bootstrap-datepicker/datepicker.js') }}></script>
<script type="text/javascript" src={{ asset('assets/lightbox/lightbox-2.6.min.js') }}></script>
<script type="text/javascript">
    $(document).ready(function() {
        $('input[name="purchase_date"]').daterangepicker({
            format:"yyyy/MM/dd"
        });
        $( ".datepicker" ).datepicker({
//            startDate: "-3m",
//            endDate: "+3m",
//            minDate: null,
//            maxDate: null,
            defaultDate: "+0d",
            format: "yyyy/mm/dd",
            autoclose: true
        });
        $('#chassis_no').focus();
    });

    function enableDisable(bEnable, textBoxID) {
        document.getElementById(textBoxID).disabled = bEnable;
        document.getElementById(textBoxID).value = "";
    }

    function setAllFaxFlag(bEnable) {
        $('.fax_chbox').prop("checked", bEnable);
    }

function faxSend() {
    var sender = $('select[name=fax_account]').val();
    var subject = $('#subject').val();
    var email_body = $('#email_body').val();
    var email_signature = $('#email_signature').val();

    var cars = new Array();
    cars = $('input:checkbox:checked.fax_chbox').map(function () {
        return (this.id).substring(4);
    }).get();
    if (cars.length == 0) {
        // the array is defined and has at least one element
        alert('Та ядаж нэг машин сонгоно уу?');
    } else {

        if (confirm('送信してもよろしいですか？はい、いいえ')) {
            var request = $.ajax({
                url: '{{ route('car_balance.main.fax_send') }}',
                type: "get",
                data: {cars: cars, sender: sender, subject: subject, email_body: email_body, email_signature: email_signature},
                dataType: "JSON"
            });

            request.done(function(result) {
                window.location.href = '{{ URL::route('car_balance.main.fax_transmission_list') }}';
                //console.log(result.status, result.success_car);
            });
        }
    }
}

$(document).ready(function() {
    if ($('select[name=fax_account]').val()){
        $.get('{{ route('car_balance.main.getFaxDetial') }}?id='+$('select[name=fax_account]').val(), function(d){
            document.getElementById('subject').value = d.fax_subject;
            document.getElementById('email_body').value = d.fax_body;
            document.getElementById('email_signature').value = d.fax_signature;
        })
    }
    $('select[name=fax_account]').on('change', function(e){
        $.get('{{ route('car_balance.main.getFaxDetial') }}?id='+this.value, function(d){
            document.getElementById('subject').value = d.fax_subject;
            document.getElementById('email_body').value = d.fax_body;
            document.getElementById('email_signature').value = d.fax_signature;
        })
    });
});



</script>
@stop